/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import net.bioteam.appweb.Config;

public class UinxOSSubmiter extends LocalBaseCommandSubmitter
{

	Random random=new Random(System.currentTimeMillis());
	
	protected String generateScriptFile(UnfinishedJob job, ProgramValueBean bean, String binaryName) throws IOException
	{
		String fileName = job.getWorkdir() + File.separator + SCRIPT_FILE;
		FileWriter writer = new FileWriter(fileName);
		writer.write("#!/bin/sh\n");
		writer.write(Config.envstr);
		writer.write("{\n");
		if (binaryName!=null)
		{
			writer.write("APP=`which "+binaryName+"`\n");
		}
		writer.write(job.getCmdline());
		writer.write("\n} 1>job_out.txt 2>job_error.txt\n");
		writer.write("status=$?\n");
		writer.write("\necho $status > " + FINISH_MARK + "\n");
		writer.write("\nexit $status\n");
		writer.close();

		return fileName;
	}

	

	protected String submitScript(String filename, UnfinishedJob job, ProgramValueBean bean) throws IOException
	{
		ProcessBuilder pb = new ProcessBuilder(filename);
		// Map<String, String> env = pb.environment();
		// env.put("VAR1", "myValue");
		// env.remove("OTHERVAR");
		// env.put("VAR2", env.get("VAR1") + "suffix");
		pb.directory(new File(job.getWorkdir()));
		pb.start();
		return String.valueOf(random.nextLong());
	}

	
	@Override
	protected String generatePostScriptFile(UnfinishedJob job, ProgramValueBean bean, String postscript) throws IOException
	{
		throw new UnsupportedOperationException("post command  is not implemented for Unix OS");
	}



	@Override
	protected String submitPostScript(String filename, UnfinishedJob job, ProgramValueBean bean, String parentJobId) throws Exception
	{
		throw new UnsupportedOperationException("post command  is not implemented for Unix OS");
	}



	public void killJob(RunStatus rs) throws Exception
	{
		throw new Exception("kill job is not supported");
		
	}
}
